package my.jwds.p91;

import java.util.HashSet;
import java.util.Set;

public class NumDecodings {

    public static void main(String[] args) {
        NumDecodings obj = new NumDecodings();
        System.out.println(obj.numDecodings("22"));
    }
    static Set<String> set = new HashSet<>();

    static{
        for (int i = 1; i <= 26; i++) {
            set.add(i+"");
        }
    }
    public int numDecodings(String s) {
        int length = s.length();
        int[]dp = new int[length];
        dp[0] = contains(s,0,1) ? 1:0;
        if (s.length() == 1){
            return dp[0];
        }
        dp[1] = (contains(s,1,2) ? dp[0]:0) + (contains(s,0,2) ?1:0);
        for (int i = 2; i < length; i++) {
            if (contains(s,i,i+1)){
                dp[i]+=dp[i-1];
            }
            if (contains(s,i-1,i+1)){
                dp[i]+=dp[i-2];
            }
        }
        return dp[length - 1];
    }

    private boolean contains(String s,int start,int end){
        return set.contains(s.substring(start,end));
    }
}